<h1 class="title-h1">帮会列表</h1>
<div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="app-content-list">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-inline">
                <input type="text" name="name" placeholder="请输入姓名" autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-inline">
                <input type="text" name="phone" placeholder="请输入手机号" autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label">所属客服</label>
            <div class="layui-input-inline">
                <select name="csid">
                    <option value=""></option>
                    {volist name="cs_list" id="vo"}
                    <option value="{$vo.id}">{$vo.username}</option>
                    {/volist}
                </select>
            </div>
            <label class="layui-form-label">时间范围</label>
            <div class="layui-input-inline">
                <input type="text" name="date" id="date" class="layui-input">

            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layuiadmin-btn-list" lay-submit="" lay-filter="search" id="search">
                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i> 搜索
            </button>
        </div>
    </div>
</div>

<div class="layui-card-body">
    <table class="layui-table" id="dataTable" lay-filter="dataTable">
    </table>
    
    <script type="text/html" id="dataTableBar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <script type="text/html" id="switchTpl">
        <input type="checkbox" name="disable" lay-filter="disable" data-gang-id="{{d.id}}" lay-skin="switch"  lay-text="开通|关闭" {{d.disable==0?'checked':''}}>
    </script>

    <script type="text/html" id="moneyTpl">
        ￥{{d.sale||0}}
      </script>
    <script type="text/html" id="wxTpl">
        {{d.user_name}}({{d.wx_number}})
      </script>
    <script type="text/html" id="wxQrCodeTpl">
        <img src=" {{d.wx_qrcode}}" alt="" style="max-width:400px;height:400px;">
      </script>

</div>
<style>
    .layui-table-tips-main {
        max-height: 500px;
    }
</style>
<script>
    layui.use(['table', 'http', 'form', 'laydate'], function () {
        var table = layui.table,
            layer = layui.layer,
            http = layui.http,
            $ = layui.$;
        var form = layui.form;
        var laydate = layui.laydate;
        laydate.render({
            elem: '#date',
            range: '~',
            done: function (value, date, endDate) {
                setTimeout(() => {
                    $("#search").click()
                }, 100);
            }
        });
        form.on('submit(search)', function (data) {
            reloadTable(data.field)
            return false;
        });
        form.on('switch(disable)', function (data) {
            var gangId = $(data.elem).data('gang-id')
            http.post('/gang/gangopen', {
                'id': gangId,
                'disable': data.elem.checked
            })
        });
        table.render({
            elem: '#dataTable',
            url: '/gang/index/',
            toolbar: '#dataTableToolbar',
            page: true,
            limit: 20,
            limits: [20, 50, 100, 500],
            cols: [
                [{
                    field: 'name',
                    title: '帮会名称',
                }, {
                    templet: '#wxTpl',
                    title: '微信号',
                }, {
                    templet: '#wxQrCodeTpl',
                    title: '微信二维码',
                }, {
                    field: 'phone',
                    title: '联系电话',
                }, {
                    field: 'ct',
                    title: '申请时间',
                }, {
                    field: 'cs_name',
                    title: '所属客服',
                }, {
                    field: 'type_name',
                    title: '帮会分类',
                }, {
                    field: 'sale',
                    templet: '#moneyTpl',
                    title: '销售额',
                }, {
                    field: 'member_num',
                    title: '会员数',
                }, {
                    templet: '#switchTpl',
                    title: '开通/关闭',
                }, {
                    title: '操作',
                    toolbar: '#dataTableBar',
                    minWidth: 150,
                    fixed: 'right'
                }]
            ],
            parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.message,
                    "count": res.data.count,
                    "data": res.data.data
                };
            }
        });
        table.on('toolbar(dataTable)', function (obj) {
            switch (obj.event) {
                case 'add':
                    editFunc()
                    break;
            };
        });
        var reloadTable = function (where, sort) {
            table.reload("dataTable", {
                initSort: sort,
                where: where,
            });
        }
        var editFunc = function (id) {
            http.get('/gang/gangedit?id=' + id,id ? '编辑帮会' : '添加帮会', ['750px','600px']);
        }
        //监听行工具事件
        table.on('tool(dataTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除这条数据吗?', {
                    skin: 'layui-layer-admin',
                }, function (index) {
                    console.log(index)
                    http.post('/gang/gangdel', {
                        id: data.id
                    }, function (res) {
                        obj.del();
                        layer.close(index);
                    })
                });
            } else if (obj.event === 'edit') {
                editFunc(data.id)
            }
        });
        table.on('sort(dataTable)', function (obj) {
            reloadTable({
                field: obj.field,
                order: obj.type
            }, obj)
        });
    });
</script>